package cn.stylefeng.guns.modular.business.app.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.app.controller.dto.CreatePayOrderDTO;
import cn.stylefeng.guns.modular.business.app.controller.dto.GetPayStatusDTO;
import cn.stylefeng.guns.modular.business.app.controller.req.BankCodeReq;
import cn.stylefeng.guns.modular.business.app.controller.vo.*;
import cn.stylefeng.guns.modular.business.app.mapper.PayOrderMapper;
import cn.stylefeng.guns.modular.business.app.pojo.PayOrder;
import cn.stylefeng.guns.modular.business.app.service.ApiPayService;
import cn.stylefeng.guns.modular.business.frequency.finance.entity.Finance;
import cn.stylefeng.guns.modular.business.frequency.finance.mapper.FinanceMapper;
import cn.stylefeng.guns.modular.business.frequency.orderfinish.model.param.OrderFinishRecordVO;
import cn.stylefeng.guns.modular.business.frequency.orderfinishrecord.service.OrderFinishRecordService;
import cn.stylefeng.guns.modular.business.frequency.ordersub.entity.OrderSub;
import cn.stylefeng.guns.modular.business.frequency.ordersub.mapper.OrderSubMapper;
import cn.stylefeng.guns.modular.business.util.HttpUtil;
import cn.stylefeng.guns.sys.modular.user.constant.ApiPayConstant;
import cn.stylefeng.guns.sys.modular.user.entity.BankCode;
import cn.stylefeng.guns.sys.modular.user.entity.EnterpriseUserMsg;
import cn.stylefeng.guns.sys.modular.user.entity.SysTenant;
import cn.stylefeng.guns.sys.modular.user.mapper.BankCodeMapper;
import cn.stylefeng.guns.sys.modular.user.mapper.EnterpriseUserMsgMapper;
import cn.stylefeng.guns.sys.modular.user.service.SysTenantService;
import cn.stylefeng.guns.sys.modular.user.utils.ApiPayUtil;
import cn.stylefeng.guns.sys.modular.user.utils.param.PayParam;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huifu.bspay.sdk.opps.core.utils.DateTools;
import com.huifu.bspay.sdk.opps.core.utils.SequenceTools;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ApiPayServiceImpl implements ApiPayService {

    @Autowired
    private BankCodeMapper bankCodeMapper;

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private FinanceMapper financeMapper;

    @Autowired
    private OrderSubMapper orderSubMapper;

    @Autowired
    private OrderFinishRecordService orderFinishRecordService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EnterpriseUserMsgMapper enterpriseUserMsgMapper;

    @Value("${notify.url}")
    private String url;

    @Value("${wx.applet.appId}")
    private String appId;

    @Value("${wx.applet.appSecret}")
    private String appSecret;

    @Autowired
    private HttpUtil httpUtil;

    /**
     * 获取省市编码信息
     */
    @Override
    public ResponseData getProvinceAndCityCode() {
        //获取省市编码常量并转化为Json数组返回
        return ResponseData.success(JSONObject.parseArray(ApiPayConstant.PROVINCE_AND_CITY));
    }

    /**
     * 获取银行代码
     */
    @Override
    public ResponseData getBankCode() {
        //查询银行代码表全部数据并返回
        List<BankCode> bankCodes = bankCodeMapper.selectList(null);
        //将BankCode对象转换为BankCodeReq对象返回
        return ResponseData.success(bankCodes.stream().map(s -> {
            BankCodeReq bankCodeReq = new BankCodeReq();
            BeanUtils.copyProperties(s, bankCodeReq);
            return bankCodeReq;
        }).collect(Collectors.toList()));
    }


    /**
     * 创建支付订单
     */
    public ResponseData<CreatePayOrderDTO> createPayOrder(PayOrderVO payOrderVO) {
        //判断当前租户有没有开通汇付，如果没有必须先开通汇付
        List<Long> orderIdList = payOrderVO.getOrderIdList();
        Long tenantId = null;
        Long userId = null;
        if (!CollectionUtils.isEmpty(orderIdList)) {
            Long l = orderIdList.get(0);
            Finance finance = financeMapper.selectOne(new LambdaQueryWrapper<Finance>()
                    .eq(Finance::getOrderId, l));
            tenantId = finance.getTenantId();
            userId = finance.getCreateUser();
        } else {
            tenantId = LoginContextHolder.me().getSysLoginUser().getTenantId();
        }
        payOrderVO.setTenantId(tenantId);

        EnterpriseUserMsg enterpriseUserMsg = enterpriseUserMsgMapper.selectOne(new LambdaQueryWrapper<EnterpriseUserMsg>()
                .eq(EnterpriseUserMsg::getTenantId, tenantId));
        if (enterpriseUserMsg == null) {
            return ResponseData.error(500, "请先开通汇付！");
        }

        //创建支付参数对象，并补齐相关属性值
        PayParam payParam = createPayParam(payOrderVO);

        if (payParam == null) {
            return ResponseData.error(500, "参数有误！");
        }

        //将支付信息存入数据库
        PayOrder payOrder = savePayToDatabase(payParam, userId, tenantId);

        //调用第三方接口完成订单创建
        return doCreatePayOrder(payParam);
    }

    /**
     * 指定金额完成支付
     */
    @Override
    public ResponseData<CreatePayOrderDTO> paymentOfSpecifiedAmount(PaymentOfSpecifiedAmountVO paymentOfSpecifiedAmountVO) {


        //判断如果指定金额大于到付未付则返回错误信息
        Finance finance = financeMapper.selectOne(new LambdaQueryWrapper<Finance>()
                .eq(Finance::getOrderId, paymentOfSpecifiedAmountVO.getOrderId()));
        BigDecimal deliveryUn = finance.getDeliveryUn();

        //对deliveryUn进行非空校验
        if (Objects.isNull(deliveryUn)) {
            return ResponseData.error(500, "未付金额为null");
        }

        if (Double.parseDouble(paymentOfSpecifiedAmountVO.getTransAmt()) >
                Double.parseDouble(deliveryUn.toString())) {
            return ResponseData.error(500, "交易金额不能大于未付金额");
        }

        //创建PayOrderVO对象
        PayOrderVO payOrderVO = new PayOrderVO();

        //将paymentOfSpecifiedAmountVO属性值拷贝到payOrderVO
        BeanUtils.copyProperties(paymentOfSpecifiedAmountVO, payOrderVO);

        //赋值订单id
        List<Long> orderIdList = new ArrayList<>();
        orderIdList.add(paymentOfSpecifiedAmountVO.getOrderId());
        payOrderVO.setOrderIdList(orderIdList);
        payOrderVO.setOrderNo(paymentOfSpecifiedAmountVO.getOrderNo());
        //设置订单类型
        payOrderVO.setPayType(1);

        //完成订单创建
        return createPayOrder(payOrderVO);
    }

    /**
     * 根据订单完成支付
     */
    @Override
    public ResponseData<CreatePayOrderDTO> payByOrder(OrderFinishRecordVO orderFinishRecordVO) {
        //判断如果当前订单已经支付成功则阻止再次支付
        //将订单id使用逗号分隔
        String orderIdStr = commaSeparatedOrder(orderFinishRecordVO.getOrderIdList());
        List<PayOrder> payOrders = payOrderMapper.selectList(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderId, orderIdStr)
                .eq(PayOrder::getPayType, 2));
        if (!CollectionUtils.isEmpty(payOrders)) {
            for (PayOrder payOrder : payOrders) {
                if (1 == payOrder.getStatus()) {
                    return ResponseData.error(500, "当前订单已经支付完成！");
                }
            }
        }
        //创建PayOrderVO对象
        PayOrderVO payOrderVO = new PayOrderVO();

        //根据多个订单id查询订单总金额
        List<Long> orderIdList = orderFinishRecordVO.getOrderIdList();
        List<OrderSub> orderSubList = orderSubMapper.selectBatchIds(orderIdList);
        if (orderSubList != null && orderSubList.size() > 0) {
            BigDecimal bigDecimal = new BigDecimal("0");
            for (OrderSub orderSub : orderSubList) {
                BigDecimal deliveryPayFee = orderSub.getDeliveryPayFee();
                if (deliveryPayFee == null) {
                    deliveryPayFee = new BigDecimal("0");
                }
                bigDecimal = bigDecimal.add(deliveryPayFee);
            }
            String s = bigDecimal.toString();
            double aDouble = Double.parseDouble(s);
            if (0 == aDouble) {
                return ResponseData.error(500, "订单金额必须大于0");
            }
            payOrderVO.setTransAmt(s);
        }

        //设置订单类型
        payOrderVO.setPayType(2);
        payOrderVO.setOrderIdList(orderFinishRecordVO.getOrderIdList());
        payOrderVO.setTradeType(orderFinishRecordVO.getTradeType());
        payOrderVO.setGoodsDesc(orderFinishRecordVO.getGoodsDesc());
        payOrderVO.setOpenId(orderFinishRecordVO.getOpenId());
        payOrderVO.setOrderNo(orderFinishRecordVO.getOrderNo());
        //完成订单创建
        ResponseData<CreatePayOrderDTO> payOrder = createPayOrder(payOrderVO);

        //先将交付完成所需信息存入Redis中
        redisTemplate.opsForValue().set(ApiPayConstant.DELIVERY_COMPLETED + "_" + orderFinishRecordVO.getOrderIdList().get(0), orderFinishRecordVO);

        return payOrder;
    }

    /**
     * 根据订单唯一标识查询订单支付状态
     */
    @Override
    public ResponseData<GetPayStatusDTO> getPayStatus(GetPayStatusVO getPayStatusVO) {
        //创建响应对象
        GetPayStatusDTO getPayStatusDTO = new GetPayStatusDTO();

        //获取请求日期和请求流水号
        String reqDate = getPayStatusVO.getReqDate();
        String reqSeqId = getPayStatusVO.getReqSeqId();

        //查询数据库
        PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getReqDate, reqDate)
                .eq(PayOrder::getReqSeqId, reqSeqId));

        //获取订单状态并设置到响应对象中
        Integer status = payOrder.getStatus();
        getPayStatusDTO.setStatus(status.toString());

        //响应
        return ResponseData.success(getPayStatusDTO);
    }

    /**
     * 在修改订单页面完成现付-线上支付
     */
    @Override
    public ResponseData<CreatePayOrderDTO> payByUpdateOrder(PayByUpdateOrderVO payByUpdateOrderVO) {
        //判断当前订单是否已经支付
        List<PayOrder> payOrders = payOrderMapper.selectList(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderId, payByUpdateOrderVO.getOrderId())
                .eq(PayOrder::getPayType, 4));
        if (!CollectionUtils.isEmpty(payOrders)) {
            for (PayOrder payOrder : payOrders) {
                if (1 == payOrder.getStatus()) {
                    return ResponseData.error(500, "当前订单已经支付完成！");
                }
            }
        }

        PayOrderVO param = new PayOrderVO();
        param.setGoodsDesc(payByUpdateOrderVO.getGoodsDesc());
        List<Long> list = new ArrayList<>();
        list.add(payByUpdateOrderVO.getOrderId());
        param.setOrderIdList(list);
        param.setPayType(4);
        param.setTradeType(payByUpdateOrderVO.getTradeType());
        //根据订单id查询现付金额
        Finance finance = financeMapper.selectOne(new LambdaQueryWrapper<Finance>()
                .eq(Finance::getOrderId, payByUpdateOrderVO.getOrderId()));
        param.setTransAmt(finance.getSpotUn().toString());
        param.setOpenId(payByUpdateOrderVO.getOpenId());
        param.setOrderNo(payByUpdateOrderVO.getOrderNo());
        return createPayOrder(param);
    }

    /**
     * 完成欠付支付
     *
     * @param payByUpdateOrderVO
     * @return
     */
    @Override
    public ResponseData<CreatePayOrderDTO> payBackOrder(PayByUpdateOrderVO payByUpdateOrderVO) {
        //判断当前订单是否已经支付
        List<PayOrder> payOrders = payOrderMapper.selectList(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderId, payByUpdateOrderVO.getOrderId())
                .eq(PayOrder::getPayType, 5));
        if (!CollectionUtils.isEmpty(payOrders)) {
            for (PayOrder payOrder : payOrders) {
                if (1 == payOrder.getStatus()) {
                    return ResponseData.error(500, "当前订单已经支付完成！");
                }
            }
        }

        PayOrderVO param = new PayOrderVO();
        param.setGoodsDesc(payByUpdateOrderVO.getGoodsDesc());
        List<Long> list = new ArrayList<>();
        list.add(payByUpdateOrderVO.getOrderId());
        param.setOrderIdList(list);
        param.setPayType(5);
        param.setTradeType(payByUpdateOrderVO.getTradeType());
        //根据订单id查询欠付金额
        Finance finance = financeMapper.selectOne(new LambdaQueryWrapper<Finance>()
                .eq(Finance::getOrderId, payByUpdateOrderVO.getOrderId()));
        param.setTransAmt(finance.getBackUn().toString());
        param.setOpenId(payByUpdateOrderVO.getOpenId());
        return createPayOrder(param);
    }

    /**
     * 在创建订单生成支付宝或微信支付信息
     */
    @Override
    public ResponseData<CreatePayOrderDTO> getPayInfo(GetPayInfoVO getPayInfoVO) {
        //判断如果当前订单已经支付成功则阻止再次支付
        //将订单id使用逗号分隔
        String orderIdStr = commaSeparatedOrder(getPayInfoVO.getOrderIdList());
        List<PayOrder> payOrders = payOrderMapper.selectList(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderId, orderIdStr)
                .eq(PayOrder::getPayType, 3));
        if (!CollectionUtils.isEmpty(payOrders)) {
            for (PayOrder payOrder : payOrders) {
                if (1 == payOrder.getStatus()) {
                    return ResponseData.error(500, "当前订单已经支付完成！");
                }
            }
        }

        PayOrderVO payOrderVO = new PayOrderVO();
        BeanUtils.copyProperties(getPayInfoVO, payOrderVO);

        //根据订单id查询总金额
        List<OrderSub> orderSubList = orderSubMapper.selectBatchIds(getPayInfoVO.getOrderIdList());

        BigDecimal bigDecimal = new BigDecimal("0");
        for (OrderSub orderSub : orderSubList) {
            BigDecimal spotPayFee = orderSub.getSpotPayFee();
            bigDecimal = bigDecimal.add(spotPayFee);
        }
        payOrderVO.setTransAmt(bigDecimal.toString());
        payOrderVO.setPayType(3);
        payOrderVO.setOrderNo(getPayInfoVO.getOrderNo());
        return createPayOrder(payOrderVO);
    }

    /**
     * 逗号分隔订单
     */
    private String commaSeparatedOrder(List<Long> orderIdList) {
        //如果集合为空返回null
        if (CollectionUtils.isEmpty(orderIdList)) {
            return null;
        }
        //创建StringJoiner对象
        StringJoiner sj = new StringJoiner(",");
        //遍历订单集合
        for (Long orderId : orderIdList) {
            sj.add(orderId + "");
        }
        //返回结果
        return sj.toString();
    }

    /**
     * 获取OpenId
     */
    @Override
    public ResponseData getOpenId(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session" +
                "?appid=" + appId +
                "&secret=" + appSecret +
                "&js_code=" + code +
                "&grant_type=authorization_code";
        String res = httpUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(res);
        String openid = jsonObject.getString("openid");
        return ResponseData.success(openid);
    }


    /**
     * 调用第三方接口完成订单创建
     */
    private ResponseData<CreatePayOrderDTO> doCreatePayOrder(PayParam payParam) {

        //设置正确的支付金额格式
        String transAmt = payParam.getTransAmt();
        if (transAmt.contains(".")) {
            String[] split = transAmt.split("\\.");
            if (split[1].length() == 1) {
                payParam.setTransAmt(transAmt + "0");
            } else if (split[1].length() > 2) {
                payParam.setTransAmt(split[0] + "." + split[1].substring(0, 2));
            }
        } else {
            payParam.setTransAmt(transAmt + ".00");
        }

        //调用第三方接口
        try {
            Map<String, Object> payResultMap = ApiPayUtil.pay(payParam);
            //如果下单失败，抛出异常
            if (!"00000100".equals(payResultMap.get("resp_code"))) {
                throw new ServiceException(500, (String) payResultMap.get("resp_desc"));
            }
            //下单成功，封装返回的信息
            CreatePayOrderDTO createPayOrderDTO = new CreatePayOrderDTO();

            //给属性赋值
            createPayOrderDTO.setTransAmt((String) payResultMap.get("trans_amt"));
            createPayOrderDTO.setQrCode((String) payResultMap.get("qr_code"));
            createPayOrderDTO.setReqDate((String) payResultMap.get("req_date"));
            createPayOrderDTO.setReqSeqId((String) payResultMap.get("req_seq_id"));

            //如果小程序的支付信息不为空，将其返回
            String payInfo = (String) payResultMap.get("pay_info");
            if (StringUtils.isNotEmpty(payInfo)) {
                createPayOrderDTO.setPayInfo(payInfo);
            }

            //返回
            return ResponseData.success(createPayOrderDTO);

        } catch (Exception e) {
            e.printStackTrace();
            //抛出异常
            throw new ServiceException(500, e.getMessage());
        }
    }

    /**
     * 将支付信息存入数据库
     */
    private PayOrder savePayToDatabase(PayParam payParam, Long userId, Long tenantId) {

        //创建实体类对象
        PayOrder payOrder = new PayOrder();

        //属性拷贝
        BeanUtils.copyProperties(payParam, payOrder);

        //属性填充
        if (Objects.isNull(userId)) {
            userId = LoginContextHolder.me().getSysLoginUserId();
        }
        payOrder.setCreateBy(userId);
        payOrder.setUpdateBy(userId);
        payOrder.setCreateTime(LocalDateTime.now());
        payOrder.setUpdateTime(LocalDateTime.now());

        //将交易状态设置为处理中
        payOrder.setStatus(0);

        //设置未分账
        payOrder.setIsDivideAcc(0);

        //租户id
        payOrder.setTenantId(tenantId);

        //进行判断，如果是微信相关支付，更新表中记录，支付宝新增
        String tradeType = payParam.getTradeType();

        //对支付类型进行判断
        if ("T_JSAPI".equals(tradeType)) {

            //获取订单唯一标识
            String orderNo = payParam.getOrderNo();

            //如果为空直接抛出异常
            if (StringUtils.isEmpty(orderNo)) {
                throw new ServiceException(500, "订单唯一标识为空");
            }

            //根据订单唯一标识查询表记录
            PayOrder payOrderB = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));

            //如果为空抛出异常
            if (Objects.isNull(payOrderB)) {
                throw new ServiceException(500, "未查询到订单记录");
            }

            //根据订单id更新记录
            payOrder.setId(payOrderB.getId());

            //执行更新
            payOrderMapper.updateById(payOrder);
        } else {

            //执行新增
            payOrderMapper.insert(payOrder);
        }
        return payOrder;
    }


    /**
     * 创建支付参数对象，并补齐相关属性值
     */
    private PayParam createPayParam(PayOrderVO payOrderVO) {
        //参数校验
        if (Objects.isNull(payOrderVO)) {
            return null;
        }
        //创建支付参数对象
        PayParam payParam = new PayParam();

        //属性拷贝
        BeanUtils.copyProperties(payOrderVO, payParam);

        //订单id
        payParam.setOrderId(String.valueOf(payOrderVO.getOrderIdList().get(0)));

        //如果支付类型是3或2需要将订单集合转换为字符串并使用逗号间隔
        if (payOrderVO.getPayType() == 3 || payOrderVO.getPayType() == 2) {
            List<Long> orderIdList = payOrderVO.getOrderIdList();
            if (Objects.nonNull(orderIdList) && orderIdList.size() > 0) {
                StringJoiner joiner = new StringJoiner(",");
                for (Long aLong : orderIdList) {
                    joiner.add(aLong.toString());
                }
                payParam.setOrderId(joiner.toString());
            }
        } else if (payOrderVO.getPayType() == 4 || payOrderVO.getPayType() == 1) {
            payParam.setOrderId(payOrderVO.getOrderIdList().get(0).toString());
        }

        //补齐属性
        payParam.setReqDate(DateTools.getCurrentDateYYYYMMDD());
        payParam.setReqSeqId(SequenceTools.getReqSeqId32());
        payParam.setHuifuId(getHuifuId(payOrderVO.getTenantId()));
        payParam.setNotifyUrl(url);

        //转换交易类型
        String tradeType = payOrderVO.getTradeType();

        //如果类型是3 或者 4 统一为A_NATIVE
        if ("3".equals(tradeType) || "4".equals(tradeType)) {
            payParam.setTradeType("A_NATIVE");
        } else if ("1".equals(tradeType)) {
            payParam.setTradeType("T_MINIAPP");
        } else if ("2".equals(tradeType)) {
            payParam.setTradeType("T_JSAPI");
        }

        //返回
        return payParam;
    }

    @Autowired
    private SysTenantService sysTenantService;

    public String getHuifuId(Long tenantId) {
        SysTenant sysTenant = sysTenantService.getById(tenantId);
        if (sysTenant == null) {
            return null;
        }
        return sysTenant.getHuifuId();
    }

    /**
     * 根据订单id查询订单支付状态
     *
     * @param getPayStatusByOrderIdVo
     * @return
     */
    @Override
    public ResponseData<GetPayStatusDTO> getPayStatusByOrderId(GetPayStatusByOrderIdVo getPayStatusByOrderIdVo) {
        List<String> ids = getPayStatusByOrderIdVo.getIds();
        //获取类型
        String type = getPayStatusByOrderIdVo.getType();

        if (CollectionUtils.isEmpty(ids)) {
            return ResponseData.error(500, "订单id不能为空");
        }

        if (ids.size() == 1) {
            String orderId = ids.get(0);
            return getOrderPayStatus(orderId, type);
        } else {
            StringJoiner sj = new StringJoiner(",");
            for (String id : ids) {
                sj.add(id);
            }
            return getOrderPayStatus(sj.toString(), type);
        }
    }

    /**
     * 在微信支付前生成一个唯一标识
     *
     * @return
     */
    @Override
    public ResponseData getPayId() {
        //1、使用UUID生成唯一标识
        String uuid = UUID.randomUUID().toString();

        //2、先根据唯一标识查询数据库，如果能够查到数据，表示该UUID不唯一，重新获取
        List<PayOrder> payOrders = payOrderMapper.selectList(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderNo, uuid));
        if (CollectionUtil.isNotEmpty(payOrders)) {
            uuid = UUID.randomUUID().toString();
        }

        //3、将UUID存入数据库
        PayOrder payOrder = new PayOrder();
        payOrder.setOrderNo(uuid);
        payOrderMapper.insert(payOrder);

        //4、返回UUID
        return ResponseData.success(uuid);
    }

    /**
     * 根据订单唯一标识查询订单支付状态
     *
     * @param getWxPayStatusVO
     * @return
     */
    @Override
    public ResponseData<GetPayStatusDTO> getWxPayStatus(GetWxPayStatusVO getWxPayStatusVO) {
        //1、获取订单唯一标识
        String orderNo = getWxPayStatusVO.getOrderNo();

        //2、根据订单唯一标识查询数据库
        PayOrder payOrder = payOrderMapper.selectOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));

        GetPayStatusDTO getPayStatusDTO = new GetPayStatusDTO();
        getPayStatusDTO.setStatus("0");

        //3、如果查询到结果为null将订单状态设置为处理中，如果不为空按照实际值返回
        if (Objects.nonNull(payOrder) && Objects.isNull(payOrder.getStatus())) {

            return ResponseData.success(getPayStatusDTO);
        }
        if (Objects.nonNull(payOrder)) {
            getPayStatusDTO.setStatus(payOrder.getStatus() + "");
            return ResponseData.success(getPayStatusDTO);
        }
        return ResponseData.success(getPayStatusDTO);
    }


    private ResponseData getOrderPayStatus(String orderId, String type) {
        GetPayStatusDTO getPayStatusDTO = new GetPayStatusDTO();
        List<PayOrder> payOrders = payOrderMapper.selectList(new LambdaQueryWrapper<PayOrder>()
                .eq(PayOrder::getOrderId, orderId)
                .eq(PayOrder::getPayType, Integer.valueOf(type))
                .orderByDesc(PayOrder::getCreateTime));
        if (CollectionUtils.isEmpty(payOrders)) {
            getPayStatusDTO.setStatus("0");
            return ResponseData.success(getPayStatusDTO);
        }

        getPayStatusDTO.setStatus(payOrders.get(0).getStatus() + "");
        return ResponseData.success(getPayStatusDTO);
    }
}
